import Vue from 'vue'
import Vuex from 'vuex'

import { LoginAPI } from '@/api'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: localStorage.getItem('token') || ''
  },
  getters: {
  },
  mutations: {
    getToken (state, val) {
      state.token = val
      localStorage.setItem('token', state.token)
    }
  },
  actions: {
    async setToken (store, val) {
      try {
        const res = await LoginAPI(val)
        const token = res.data.body.token
        store.commit('getToken', token)
        return Promise.resolve(res)
      } catch (err) {
        console.log(err)
        return Promise.reject(err)
      }
    }
  },
  modules: {
  }
})
